package cn.exrick.xboot.modules.IsFinishedCourse.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.IsFinishedCourse.entity.IsFinishedCourse;
import cn.exrick.xboot.modules.IsFinishedCourse.service.IIsFinishedCourseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xbw
 */
@Slf4j
@RestController
@Api(description = "在线教育课程完成情况表管理接口")
@RequestMapping("/xboot/isFinishedCourse")
@Transactional
public class IsFinishedCourseController {

    @Autowired
    private IIsFinishedCourseService iIsFinishedCourseService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<IsFinishedCourse> get(@PathVariable String id){

        IsFinishedCourse isFinishedCourse = iIsFinishedCourseService.getById(id);
        return new ResultUtil<IsFinishedCourse>().setData(isFinishedCourse);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<IsFinishedCourse>> getAll(){

        List<IsFinishedCourse> list = iIsFinishedCourseService.list();
        return new ResultUtil<List<IsFinishedCourse>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<IsFinishedCourse>> getByPage(@ModelAttribute PageVo page){

        IPage<IsFinishedCourse> data = iIsFinishedCourseService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<IsFinishedCourse>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<IsFinishedCourse> saveOrUpdate(@ModelAttribute IsFinishedCourse isFinishedCourse){

        if(iIsFinishedCourseService.saveOrUpdate(isFinishedCourse)){
            return new ResultUtil<IsFinishedCourse>().setData(isFinishedCourse);
        }
        return new ResultUtil<IsFinishedCourse>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            iIsFinishedCourseService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
}
